using System;
using System.ComponentModel;
using System.Web.UI;
namespace JQueryControlToolkit.Controls
{
    public class AutocompleteExtender : JQueryExtender
    {
        private bool dataBinding;
        private bool rewritingUrl;
        private int? maxCount;
        [JQueryElement("source", false)]
        public JSObject Source
        {
            get
            {
                return base.GetPropertyValue<JSObject>("Source", null);
            }
            set
            {
                base.SetPropertyValue<JSObject>("Source", value);
            }
        }
        [JQueryElement(null, "source", false)]
        public string Url
        {
            get
            {
                return base.GetPropertyValue<string>("Url", null);
            }
            set
            {
                base.SetPropertyValue<string>("Url", value);
                if (!this.rewritingUrl && this.dataBinding && this.maxCount.HasValue)
                {
                    this.rewritingUrl = true;
                    this.MaxCount = this.maxCount.Value;
                    this.rewritingUrl = false;
                }
            }
        }
        [JQueryEvent("source")]
        [DefaultValue(null)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public JSFunctionString OnSourceRequested
        {
            get
            {
                return base.GetPropertyValue<JSFunctionString>("OnSourceRequested", null);
            }
            set
            {
                base.SetPropertyValue<JSFunctionString>("OnSourceRequested", value);
            }
        }
        [JQueryElement("appendTo", true)]
        public string AppendTo
        {
            get
            {
                return base.GetPropertyValue<string>("AppendTo", null);
            }
            set
            {
                base.SetPropertyValue<string>("AppendTo", value);
            }
        }
        [JQueryElement("minLength", false)]
        public int MinLength
        {
            get
            {
                return base.GetPropertyValue<int>("MinLength", 3);
            }
            set
            {
                base.SetPropertyValue<int>("MinLength", value);
            }
        }

        [JQueryElement("takeFirstOnBlur", false)]
        public string TakeFirstOnBlur
        {
            get
            {
                return base.GetPropertyValue<string>("TakeFirstOnBlur", null);
            }
            set
            {
                base.SetPropertyValue<string>("TakeFirstOnBlur", value);
            }
        }

        [JQueryEvent("focus")]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [DefaultValue(null)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public JSFunctionString OnFocus
        {
            get
            {
                var value = GetPropertyValue<JSFunctionString>("OnFocus", null);
                if (value == null)
                    SetPropertyValue("OnFocus", value = new JSFunctionString());
                return value;

            }
            set { SetPropertyValue("OnFocus", value); }
        }


        public int MaxCount
        {
            get
            {
                return this.maxCount ?? 0;
            }
            set
            {
                this.maxCount = new int?(value);
                if (!string.IsNullOrEmpty(this.Url))
                {
                    this.Url = this.Url + "?count=" + value;
                }
            }
        }
        protected override string jQueryFunction
        {
            get
            {
                return "autocomplete";
            }
        }
        public override void DataBind()
        {
            this.dataBinding = true;
            base.DataBind();
            this.dataBinding = false;
        }
    }
}
